package com.lushuai.jiubeidanci.fragment;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.VideoView;
import androidx.fragment.app.Fragment;
import com.lushuai.jiubeidanci.R;
import com.lushuai.jiubeidanci.VideoPlayerActivity;

public class BookmarkFragment extends Fragment {
    
    private VideoView videoView1;
    private ImageView videoThumbnail1;
    private ImageView playButton1;
    private VideoView videoView2;
    private ImageView videoThumbnail2;
    private ImageView playButton2;
    private boolean isPlaying1 = false;
    private boolean isPlaying2 = false;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bookmark, container, false);
        
        // 初始化视图
        videoView1 = view.findViewById(R.id.video_view_1);
        videoThumbnail1 = view.findViewById(R.id.video_thumbnail_1);
        playButton1 = view.findViewById(R.id.play_button_1);
        videoView2 = view.findViewById(R.id.video_view_2);
        videoThumbnail2 = view.findViewById(R.id.video_thumbnail_2);
        playButton2 = view.findViewById(R.id.play_button_2);
        
        // 设置视频1的点击事件
        View.OnClickListener playClickListener1 = v -> {
            Intent intent = new Intent(requireActivity(), VideoPlayerActivity.class);
            intent.putExtra("video_res_id",R.raw.video1);
            startActivity(intent);
        };
        
        videoThumbnail1.setOnClickListener(playClickListener1);
        playButton1.setOnClickListener(playClickListener1);
        
        // 设置视频2的点击事件
        View.OnClickListener playClickListener2 = v -> {
            Intent intent = new Intent(requireActivity(), VideoPlayerActivity.class);
            intent.putExtra("video_res_id", R.raw.video2);
            startActivity(intent);
        };
        
        videoThumbnail2.setOnClickListener(playClickListener2);
        playButton2.setOnClickListener(playClickListener2);
        
        // 初始化所有课程卡片的点击事件
        int[] courseIds = {
            R.id.course_1, R.id.course_2, R.id.course_3,
            R.id.course_4, R.id.course_5, R.id.course_6
        };
        
        for (int id : courseIds) {
            view.findViewById(id).setOnClickListener(v -> {
                Intent intent = new Intent(requireActivity(), VideoPlayerActivity.class);
                intent.putExtra("video_res_id", R.raw.video2);
                startActivity(intent);
            });
        }
        
        return view;
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (videoView1 != null) {
            videoView1.stopPlayback();
        }
        if (videoView2 != null) {
            videoView2.stopPlayback();
        }
    }
}